package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.t9101DR.bean.RoomAddress;
import com.t9101DR.bean.wish;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.wishDao;

public class wishDaoImpl extends BaseDao implements wishDao {

	@Override
	public List<wish> list(wish w) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<wish> list=new ArrayList<wish>();
		try {
			conn=getConnection();
			String sql="SELECT wishId,name,price,URL FROM `wish` WHERE 1=1";
			if(w!=null&&w.getWishId()!=0) {
				sql=sql+" and wishId= "+w.getWishId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				wish bren=new wish();
				bren.setWishId(rs.getInt(1));
				bren.setName(rs.getString(2));
				bren.setPrice(rs.getInt(3));
				bren.setUrl(rs.getString(4));
				
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(wish w) {
		int count =0;
		String sql="INSERT INTO wish(name,price,`URL`) VALUES (?,?,?) ";
		Object [] pare= {w.getName(),w.getPrice(),w.getUrl()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int deleteById(int id) {
		int count=0;
		String sql="delete from wish where wishId=?";
		Object[] params= {id};
		count=this.executeUpdate(sql, params);
		return count;
	}
	
}
